package com.mywork.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywork.dao.CureProcessMapper;
import com.mywork.dao.CureitemMapper;
import com.mywork.dao.RecipeDiagnosisMapper;
import com.mywork.dao.RecipeDiagnosisViewMapper;
import com.mywork.dao.RecipeMapper;
import com.mywork.dao.RecipeSignMapper;
import com.mywork.dao.RecipeViewMapper;
import com.mywork.dao.UsedrugMapper;
import com.mywork.po.CureProcess;
import com.mywork.po.Cureitem;
import com.mywork.po.Page;
import com.mywork.po.Recipe;
import com.mywork.po.RecipeDiagnosis;
import com.mywork.po.RecipeSign;
import com.mywork.po.RecipeView;
import com.mywork.po.Usedrug;

@Service
public class RecipeService {
	@Autowired
	UsedrugMapper useDrugDao;
	@Autowired
	CureProcessMapper cureProcessDao;
	
	@Autowired
	CureitemMapper cureitemDao;
	@Autowired
	RecipeViewMapper recipeViewDao;
	
	@Autowired
	RecipeMapper recipeDao;
	
	@Autowired
	RecipeSignMapper recipeSignDao;
	
	@Autowired
	RecipeDiagnosisMapper recipeDiagnosisDao;
	
	public void addRecipe(Recipe recipe, String[] cureId,String[] cureCount,String[] drugId,String[] drugCount, String[] signId, String[] signStatue, String[] diagnosisId) {
		// TODO 自动生成的方法存根
		//为五张表植入数据
		//添加处方表
		recipeDao.insert(recipe);
		
		Long recipeId =recipeDao.getMaxRecipeId();
		
		//为治疗项目表添加数据
		for(int i=0;i<cureId.length;i++){
			Cureitem cureItem = new Cureitem();
			cureItem.setCureid(Integer.valueOf(cureId[i]).intValue());
			cureItem.setCuresum(Integer.valueOf(cureCount[i]).intValue());
			cureItem.setFinishsum(0);
			cureItem.setRecipeid(recipeId);
			cureitemDao.insert(cureItem);
		}
		
		//为处方用药表添加数据
		for(int i=0;i<drugId.length;i++){
			Usedrug useDrug = new Usedrug();
			useDrug.setDrugid(Long.valueOf(drugId[i]).longValue());
			useDrug.setDrugsum(Integer.valueOf(drugCount[i]).intValue());
			useDrug.setRecipeid(recipeId);
			//这里老是忘了插进去
			useDrugDao.insert(useDrug);
		}
		
		//为处方体格检查表添加数据
		for(int i=0;i<signId.length;i++){
			RecipeSign recipeSign = new RecipeSign();
			recipeSign.setRecipeid(recipeId);
			recipeSign.setSignid(Integer.valueOf(signId[i]));
			recipeSign.setStatue(Integer.valueOf(signStatue[i]));
			recipeSignDao.insert(recipeSign);
		}
		
		//为处方诊断表添加数据
		for(String diagnosis:diagnosisId){
			RecipeDiagnosis recipeDiagnosis = new RecipeDiagnosis();
			recipeDiagnosis.setDiagnosisid(Long.valueOf(diagnosis));
			recipeDiagnosis.setRecipeid(recipeId);
			recipeDiagnosisDao.insert(recipeDiagnosis);
		}
	}

	public List<RecipeView> getAllRecipe(Page page, String userId) {
		// TODO 自动生成的方法存根
		return recipeViewDao.selectAllByPage(page.getPageNo()*page.getPageSize(), page.getPageSize(), userId);
	}

	public void deleteRecipe(String recipeId) {
		// TODO 自动生成的方法存根
		recipeDao.deleteByPrimaryKey(Long.valueOf(recipeId).longValue());
	}

	public RecipeView getRecipeByRecipeId(String repiceId) {
		// TODO 自动生成的方法存根
		return recipeViewDao.selectRecipeByRecipeId(repiceId);
	}

	public void curePatien(String recipeId, String cureId, String userid) {
		// TODO 自动生成的方法存根
		//在治疗列表更新治疗最后治疗次数
		cureitemDao.updateForCurePatient(recipeId,cureId);
		//在治疗过程表里面添加流水账
		CureProcess cp = new CureProcess();
		cp.setCureid(cureId);
		cp.setDate(new Date());
		cp.setRecipeid(Long.valueOf(recipeId).longValue());
		cp.setUserid(userid);
		cureProcessDao.insert(cp);
	}

	public void revoke(String recipeId,String cureId, String cureProcessId) {
		// TODO 自动生成的方法存根
		//在治疗列表更新治疗最后治疗次数
		cureitemDao.updateForRevoke(recipeId,cureId);
		//在治疗过程表里面删除流水账
		cureProcessDao.deleteByPrimaryKey(Long.valueOf(cureProcessId).longValue());
	}

	public List<RecipeView> getRecipeBycardId(String cardId, String userId) {
		// TODO 自动生成的方法存根
		
		return recipeViewDao.selectRecipeBycardId(cardId, userId);
	}
	/**
	 * 这个是查全部人的，没有固定对象
	 * @param cardId
	 * @param userId
	 * @return
	 */
	public List<RecipeView> getRecipeBycardId(String cardId) {
		// TODO 自动生成的方法存根
		
		return recipeViewDao.selectRecipeBycardIdNoUserCheck(cardId);
	}
	
}
